package com.hp.attendance.serlvet.menu;

import com.hp.attendance.entity.Menu;
import com.hp.attendance.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addMenu")
public class MenuAddServlet extends HttpServlet {
    private MenuService menuService = new MenuService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加菜单
        try {
            String name = req.getParameter("name");
            String priority = req.getParameter("priority");
            String path = req.getParameter("path");
            String pid = req.getParameter("pid");
            Menu menu = new Menu(name, Integer.parseInt(priority), path, Integer.parseInt(pid));
            menuService.addMenu(menu);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //跳转到list列表
        req.getRequestDispatcher("/getMenuPage").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
